package com.atyjh.reggie.controller;

import com.atyjh.reggie.dto.DishDto;
import com.atyjh.reggie.dto.Result;
import com.atyjh.reggie.entity.Category;
import com.atyjh.reggie.entity.Dish;
import com.atyjh.reggie.service.IDishFlavorService;
import com.atyjh.reggie.service.IDishService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author yjh
 * @since jdk1.8.0_311 2022.06.18
 */
@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {

    @Resource
    private IDishService dishService;

    @PostMapping
    public Result<String> dish(@RequestBody DishDto dishDto) {
        return dishService.saveWithFlavor(dishDto);
    }

    @GetMapping("/page")
    public Result<Page<DishDto>> pageDish(@RequestParam("page")Integer page,
                         @RequestParam("pageSize")Integer pageSize,
                         @RequestParam(value = "name", required = false) String name) {

        return dishService.pageDish(page, pageSize, name);
    }

    @GetMapping("/{id}")
    public Result<DishDto> getDish(@PathVariable("id")Long id) {
        return dishService.getDish(id);
    }

    @PutMapping
    public Result<String> update(@RequestBody DishDto dishDto) {
        return dishService.updateWithFlavor(dishDto);
    }

    @DeleteMapping
    public Result<String> deleteBatch(@RequestParam("ids") List<Long> ids) {
        return dishService.removeBatch(ids);
    }

    @GetMapping("/list")
    public Result<List<DishDto>> list(@RequestParam("categoryId")Long categoryId) {
        return dishService.listDishDTO(categoryId);
    }

}
